﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：PurchaseOrderAdd_Step3.cs
    // 文件功能描述：新建采购单，加入采购单明细
    // 创建标识：吕海乐2009-04-27
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin.ProcureManage
{
    public partial class PurchaseOrderAdd_Step3 : BasePage
    {
        public PurchaseOrderAdd_Step3()
        {
            this.ModuleNo = Module.单据管理.ToString();
            this.PageOperateType = OperateType.新建采购单.ToString();
        }

        IBLL_Distribution id = Factory.BLL_Distribution();
        IBLL_Purchase ip = Factory.BLL_Purchase();
        IBLL_HospitalProcureCatalog hpcCtrl = Factory.BLL_HospitalProcureCatalog();

        protected void Page_Load(object sender, EventArgs e)
        {
            string orderID = Request.QueryString["orderID"];
            if (!IsPostBack)
            {
                SetAllBind();
            }
        }

        /// <summary>
        /// 绑定所有符合条件的采购单明细
        /// </summary>
        private void SetAllBind()
        {
            string hID = Request.QueryString["hID"];
            int totalCount = 0;
            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            //IList<Model.View.DistributionRelation> distributionList = id.GetDistributionByWhere(new Model.Query.DistributionRelation() { HospitalID = hID, SortFields = "AddTime DESC" }, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            //AspNetPager1.RecordCount = totalCount;
            //gvwProduceCatalog.PageSize = AspNetPager1.PageSize;
            //gvwProduceCatalog.DataSource = distributionList;
            //gvwProduceCatalog.DataBind();

            //绑定所有符合条件的目录（采购目录启用，医疗机构启用了该采购目录）
            Model.Query.HospitalProcureCatalog hospitalProcureCatalog = GetCondition(hID);

            IList<Model.View.HospitalProcureCatalog> hosiptalCatalogList = hpcCtrl.GetHospitalProcureCatalog(hospitalProcureCatalog, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            AspNetPager1.RecordCount = totalCount;
            gvwProduceCatalog.DataSource = hosiptalCatalogList;
            gvwProduceCatalog.DataBind();
        }
        /// <summary>
        /// 获取查询条件
        /// </summary>
        /// <returns></returns>
        private Model.Query.HospitalProcureCatalog GetCondition(string hID)
        {
            Model.Query.HospitalProcureCatalog hospitalProcureCatalog = new HZ.DrugTrade.Model.Query.HospitalProcureCatalog();
            hospitalProcureCatalog.HospitalID = hID;
            //hospitalProcureCatalog.Companyname_Sc = txtProduceCompany_SC.Text.Trim();
            //hospitalProcureCatalog.PinyinCode = this.txtPinyinCode.Text.Trim();
            //hospitalProcureCatalog.MedicineModelName = txtMedicineModel.Text.Trim();
            //hospitalProcureCatalog.Outlookc = txtOutLook.Text.Trim();

            //hospitalProcureCatalog.ProductName = txtProductName.Text.Trim();
            //hospitalProcureCatalog.Unit = txtUnit.Text.Trim();
            //if (HZ.Common.MyValidate.IsNumeric(txtFactor.Text.Trim()))
            //{
            //    hospitalProcureCatalog.Factor = Convert.ToInt32(txtFactor.Text.Trim());
            //}
            //hospitalProcureCatalog.Companyname_Ps = txtCompany_PS.Text.Trim();
            //hospitalProcureCatalog.Companyname_Tb = txtCompany_TB.Text.Trim();
            hospitalProcureCatalog.SortFields = "Outlookc asc";

            hospitalProcureCatalog.IsUsing = true;
            hospitalProcureCatalog.Hospital_IsUsing = true;

            //获取采购单中以后的采购目录编号
            string strAppendSql = "select procurecatalogid from  tb_PurchaseOrderDetail  where orderid ='" + Request.QueryString["orderID"] + "'";
            hospitalProcureCatalog.Append = "ProcureCatalogID Not in (" + strAppendSql + ")";

            return hospitalProcureCatalog;
        }
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            SetAllBind();
            Check();
        }

        /// <summary>
        /// 添加采购单明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            List<PurchaseOrderDetail> myList = new List<PurchaseOrderDetail>();
            for (int i = 0; i < gvwProduceCatalog.Rows.Count; i++)
            {
                System.Web.UI.HtmlControls.HtmlInputCheckBox chk = (System.Web.UI.HtmlControls.HtmlInputCheckBox)gvwProduceCatalog.Rows[i].FindControl("checkbox1");
                TextBox txtPurchaseCount = (TextBox)gvwProduceCatalog.Rows[i].FindControl("txtCount");
                Label lblPrcie = (Label)gvwProduceCatalog.Rows[i].FindControl("lblPrice");
                if (chk.Checked == true)
                {

                    //采购数量不能为空
                    if (string.IsNullOrEmpty(txtPurchaseCount.Text.Trim()))
                    {
                        HZ.Web.WebUtil.Alert("采购数量不能为空！", this);
                        txtPurchaseCount.Focus();
                        return;
                    }
                    //采购数量不能为“0”
                    if (txtPurchaseCount.Text.Trim() == "0")
                    {
                        HZ.Web.WebUtil.Alert("采购数量不能为0！", this);
                        txtPurchaseCount.Focus();
                        return;
                    }

                    //采购价格不能为空
                    if (string.IsNullOrEmpty(lblPrcie.Text.Trim()))
                    {
                        HZ.Web.WebUtil.Alert("采购价格不能为空！", this);
                        return;
                    }

                    PurchaseOrderDetail purchaseOrderDetail = new PurchaseOrderDetail();
                    purchaseOrderDetail.OrderID = Request.QueryString["orderID"];
                    purchaseOrderDetail.Price = new HZ.Common.ConvertHelper(lblPrcie.Text).ToDecimal;
                    purchaseOrderDetail.ProcureCatalogID = gvwProduceCatalog.DataKeys[i].Values[0].ToString();
                    purchaseOrderDetail.PurchaseCount = new HZ.Common.ConvertHelper(txtPurchaseCount.Text.Trim()).ToInt32;
                    purchaseOrderDetail.Amount = purchaseOrderDetail.Price * purchaseOrderDetail.PurchaseCount;
                    purchaseOrderDetail.CompanyID_PS = gvwProduceCatalog.DataKeys[i].Values[1].ToString();
                    purchaseOrderDetail.FlowID = "1";
                    myList.Add(purchaseOrderDetail);

                }
            }

            int flag = ip.AddPurchaseOrderDetail_Batch(myList);
            if (flag == 1)
            {
                SetAllBind();
                Check();
            }
            else
            {
                PageError("添加失败！", "");
            }

        }
        /// <summary>
        /// 完成采购单的制作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFinish_Click(object sender, EventArgs e)
        {
            Response.Redirect("PurchaseOrderAdd_Finish.aspx?OrderID=" + Request.QueryString["orderID"] + "");
        }

        /// <summary>
        /// 检查此产品是否已经被采购
        /// </summary>
        private void Check()
        {
            int totalCount = 0;
            HZ.DrugTrade.Model.Query.PurchaseOrderDetail qPurchaseOrderDetail = new HZ.DrugTrade.Model.Query.PurchaseOrderDetail();
            qPurchaseOrderDetail.OrderID = Request.QueryString["orderID"];
            qPurchaseOrderDetail.SortFields = "PlanTime DESC";
            IList<Model.View.PurchaseOrderDetail> purchaseOrderDetailList = ip.GetPurchaseOrderDetailByWhere(qPurchaseOrderDetail, 1, 0, out totalCount);
            for (int i = 0; i < gvwProduceCatalog.Rows.Count; i++)
            {
                for (int j = 0; j < purchaseOrderDetailList.Count; j++)
                {
                    string procureCatalogID = gvwProduceCatalog.DataKeys[i].Values[0].ToString();
                    string companyID = gvwProduceCatalog.DataKeys[i].Values[1].ToString();
                    if (purchaseOrderDetailList[j].ProcureCatalogID.Equals(procureCatalogID) && purchaseOrderDetailList[j].CompanyID_PS.Equals(companyID))
                    {
                        System.Web.UI.HtmlControls.HtmlInputCheckBox chk = (System.Web.UI.HtmlControls.HtmlInputCheckBox)gvwProduceCatalog.Rows[i].FindControl("checkbox1");
                        chk.Disabled = true;
                        break;
                    }
                }
            }
        }
    }
}
